package com.api.smoke.wall;

import org.springframework.test.context.ContextConfiguration;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.api.request.model.ApiRequestBuilder;
import com.api.test.AbstractApiTest;
import com.api.test.VoidContextLoader;
import com.api.util.AssertUtil;
import com.api.util.DataUtil;
import com.api.util.GenericUtils.GenericType;
import com.api.util.repo.DtoFactory;
import com.api.util.repo.impl.DomainUser;
import com.zyncro.api.IGroupApiService;
import com.zyncro.api.IWallApiService;
import com.zyncro.api.exceptions.ZyncroApiException;
import com.zyncro.api.model.CollectionResultApi;
import com.zyncro.api.model.EventApi;
import com.zyncro.api.model.ObjectResultApi;

@ContextConfiguration(loader = VoidContextLoader.class)
public class GetGroupEventsSmoke extends AbstractApiTest {

	private CollectionResultApi<EventApi> expectedResults;

	private String groupUrn;

	@BeforeClass
	protected void setUp() throws Exception {

		final String domainName = this.getClass().getSimpleName();
		DomainUser mainAdmin = getRepo().getMainUser(domainName);

		getApi().login(mainAdmin.getAppId(), mainAdmin.getPassword());

		groupUrn = getRepo().getPropertyDto(domainName + "-GroupUrn", new DtoFactory<String>() {

			public String createDto() {

				try {
					ApiRequestBuilder request = getRequest(IGroupApiService.class, "createGroup");
					request.setParam("name", "name");
					request.setParam("description", "description");

					ObjectResultApi<String> result = getApi().send(request);
					return result.getObject();

				} catch (Exception e) {

					throw new IllegalStateException("Exception creating test data", e);
				}
			}
		});

		String fileName = getRepo().getPropertyDto(domainName + "-Events", new DtoFactory<String>() {

			public String createDto() {

				String env = System.getProperty("env");
				String fileName = String.format("./json/%s/%s.json", env, domainName);

				try {

					String eventUrn = getApi().getService(IWallApiService.class).publishInGroup(groupUrn, "mensaje de grupo", null, null,
							null, null, null, null, false, false);

					getApi().getService(IWallApiService.class).publishInGroup(groupUrn, "mensaje de grupo 2", null, null, null, null, null,
							null, false, false);

					getApi().getService(IWallApiService.class).publishInGroup(groupUrn, "mensaje de grupo 3", null, null, null, null, null,
							null, false, false);

					getApi().getService(IWallApiService.class).publishInGroup(groupUrn, "commentario de grupo", null, null, null, eventUrn,
							null, null, false, false);

					CollectionResultApi<EventApi> result = getApi().getService(IWallApiService.class).getGroupEvents(groupUrn, null, 0,
							null, false, false, null, null);

					DataUtil.saveDtoToFile(result, fileName);
				} catch (Exception e) {

					throw new IllegalStateException("Exception creating test data", e);
				}
				return fileName;
			}
		});

		this.expectedResults = DataUtil.getDtoFromFile(fileName, new GenericType<CollectionResultApi<EventApi>>() {
		});
	}

	@Test
	public void testGetPersonalFeedEvents() throws ZyncroApiException {

		CollectionResultApi<EventApi> result = getApi().getService(IWallApiService.class).getGroupEvents(groupUrn, null, 0, null, false,
				false, null, null);

		AssertUtil.assertDTOEquals(result.getElements(), expectedResults.getElements());
	}
}
